﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ResumeSharp.Properties;
using ResumeSharp.Data;

namespace ResumeSharp
{
    public partial class LocationManager : Form
    {
        private const int LOCATION_NAME_COLUMN = 0;
        private const int COLUMN_WIDTH_OFFSET = 4;

        private ResumeSharpEntities db;
        private Account user;
        private Location currentLoc;
        private bool createNewPending = false;

        public LocationManager(ref Account userAccount)
        {
            InitializeComponent();
            UpdateLayout();
            this.user = userAccount;
        }

        private void LocationManager_Shown(object sender, EventArgs e)
        {
            if (user == null || user.IsNew)
            {
                MessageBox.Show(String.Format(Errors.GEN_USER_ACCT_REQ, this.Text));
                this.Close();
            }
            else
            {

                this.db = ((MainForm)this.MdiParent).Database;
                this.LoadLocations();
            }
        }
        private void splitMain_SplitterMoved(object sender, SplitterEventArgs e)
        {
            this.UpdateLayout();
        }
        private void lstLocations_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstLocations.SelectedItems.Count > 0)
            {
                Location l = lstLocations.SelectedItems[0].Tag as Location;
                txtLocCity.Text = l.City;
                txtLocName.Text = l.Name;
                txtLocState.Text = l.State;
                txtLocStreet.Text = l.StreetName;
                txtLocUnit.Text = l.UnitNumber;
                txtLocZip.Text = l.Postcode;
                currentLoc = l;
            }
            else
            {
                ResetForm();
            }
        }
        private void TextBox_TextChanged(object sender, EventArgs e)
        {
            cmdSave.Enabled = (txtLocName.Text.Length > 0 && currentLoc != null);
        }        
        private void cmdSave_Click(object sender, EventArgs e)
        {
            currentLoc.City = this.txtLocCity.Text;
            currentLoc.Name = this.txtLocName.Text;
            currentLoc.Postcode = this.txtLocZip.Text;
            currentLoc.State = this.txtLocState.Text;
            currentLoc.StreetName = this.txtLocStreet.Text;
            currentLoc.UnitNumber = this.txtLocUnit.Text;
            currentLoc.Account = user;
            currentLoc.Id = Guid.NewGuid();
            
            db.AddToLocation(currentLoc);
            db.SaveChanges();

            // reset the new flag after we save.
            if (createNewPending)
                createNewPending = false;

            // update the UI
            this.ResetForm();
        }
        private void cmdNew_Click(object sender, EventArgs e)
        {
            if (!createNewPending)
            {
                ResetForm();
                currentLoc = new Location();
                createNewPending = true;
            }
        }
        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void cmdDelete_Click(object sender, EventArgs e)
        {
            if (currentLoc != null)
            {
                db.DeleteObject(currentLoc);
                db.SaveChanges();
                ResetForm();
            }
        }

        #region Data Loading
        private void UpdateLayout()
        {
            lstLocations.Columns[LOCATION_NAME_COLUMN].Width = splitMain.Panel1.Width - COLUMN_WIDTH_OFFSET;
        }
        private void ResetForm()
        {
            currentLoc = null;

            
            
            LoadLocations();
        }
        private void EnableTextBoxes(bool enabled)
        {
            TextBox box;
            foreach (Control c in splitMain.Panel2.Controls)
            {
                if (c is TextBox)
                {
                    box = c as TextBox;
                    box.Clear();
                    box.Enabled = enabled;
                }
            }
        }
        private void LoadLocations()
        {
            lstLocations.BeginUpdate();
            lstLocations.Items.Clear();

            user.Location.Load();
            foreach (Location l in user.Location.OrderBy(p => p.Name))
                lstLocations.Items.Add(new ListViewItem
                {
                    Text = l.Name,
                    Tag = l
                });
            lstLocations.EndUpdate();
        }
        #endregion       
    }
}
